<?php
/**
* @link http://framework.org/
* @copyright Copyright: 2003-2004 Doutromundo
* @author Gregory Brown <bugzbrown@gmail.com>
* @access public
* @package database
*/
/*************
* GENERIC DATABASE CLASS
* The idea behind this class is to create a single interface with databases... independant of the
* DB being used.
* Availabel DB types:
* mysql
* postgres
* sqllite
* access/MSSQL (via ODBC)
* oracle
* etc...
*
*
* @package database
* @link http://framework.org/
* @version 0.0.2
* @copyright 2003-2005 Doutromundo
* @author Gregory Brown <bugzbrown@gmail.com>
* @access public
*/
class database extends dm{
	// public attributes
	/**
	 * What database whall we use - defaults to MySQL
	 *
	 * @var String
	 */
	var $db_type="mysql";
	/**
	 * ODBC connection string for when we use ODBC
	 *
	 * @var String
	 */
	var $odbc="";
	/**
	 * The server we will connect to
	 *
	 * @var String
	 */
	var $server="";
	/**
	 * Database Name
	 *
	 * @var String
	 */
	var $database="";
	/**
	 * The database user we will connect with
	 *
	 * @var String
	 */
	var $user="";
	/**
	 * Password for the database connection
	 *
	 * @var String
	 */
	var $password="";
	/**
	 * Array with the resultset
	 *
	 * @var Array
	 */
	var $resultArray;
	/**
	 * The last inserted ID in the database
	 *
	 * @var Number
	 */
	var $last_id="";
	/**
	 * Use general configuration files (defaults to TRUE)
	 *
	 * @var Boolean
	 */
	var $useConfig=true;

	// private attributes
	/**
	 * Internal DB Resource Reference
	 *
	 * @var resource
	 */
	var $db;
	/**
	 * Shall we debug all database calls? Echos a Div with return messages
	 *
	 * @var Boolean
	 */
	var $debugging=false;

	//PRIVATE FUNCTIONS
	/**
	 * Constructor
	 *
	 * @param String $dbType
	 * @param Boolean $valuesFromConfig
	 * @return database
	 */
	function database($dbType="mysql",$valuesFromConfig=true){
		$this->loadClass("database",$dbType);
		$this->db = new _database($valuesFromConfig);
		$this->resultArray=array();
	}
	/**
	 * If debugging is set to TRUE, it will echo all operations with the DB on screen
	 *
	 * @param Boolean $result
	 * @param String $operation
	 * @return Boolean
	 */
	function debug($result,$operation=""){
		if ($result){
			if ($this->debugging){
				echo "<div style='font-size:10px;font-family:arial,sans;color:#FFFFFF;background-color:#008800'>OK: ";
				echo strtoupper($this->db_type)."($operation) - [No errors computed] ";
				echo "</div>";
			}
			return true;
		}else{
			if ($this->debugging){
				echo "<div style='font-family:arial,sans;color:#FFFFFF;background-color:#ff0000;font-size:10px;'>ERROR: ";
				echo strtoupper($this->db_type)."($operation) - [".$this->db->errDesc."] ".$this->db->errDesc;
				echo "</div>";
			}
			return false;
		}

	}
	//PUBLIC FUNCTIONS
	/**
	 * Attempt a connection with the database
	 *
	 * @param Boolean $persistant
	 * @return Boolean
	 */
	function connect($persistant=false){
		if ($persistant){
			$this->db->persistant=true;
		}else{
			$this->db->persistant=false;
		}

		return $this->debug($this->db->connect(),"connect");
	}
	/**
	 * Execute a query that does not return a resultset
	 *
	 * @param String $sql
	 * @return Boolean
	 */
	function execute($sql){
		// execute query without return of variables
		$result = $this->debug($this->db->execute($sql,2),"execute");
		if ($result){
			if ($this->db->last_id>0){
				$this->last_id=$this->db->last_id;
			}
		}
		return $result;
	}
	/**
	 * Fetch a single row from the database and set it as resultArray
	 *
	 * @param String $sql
	 * @return Boolean
	 */
	function fetchSingle($sql){
	$result = $this->debug($this->db->execute($sql,0),"select single");
		if ($result){
			if (is_array($this->db->resultArray)){
				$this->resultArray=$this->db->resultArray;
			}
		}
		return $result;
	}
	/**
	 * Fetch an associative array from a query - set it to resultArray
	 *
	 * @param String $sql
	 * @return Boolean
	 */
	function fetch($sql){
		$result = $this->debug($this->db->execute($sql,1),"select results");
		if ($result){
			if (is_array($this->db->resultArray)){
				$this->resultArray=$this->db->resultArray;
			}
		}
		return $result;
	}

	/**
	 * Close connection to database
	 *
	 * @return Boolean
	 */
	function close(){
		return $this->debug($this->db->close(),"close");
	}
	/**
	*Retrieve the last Inserted ID from MySQL
	*In case of an error, the errNum and errDesc are filled and false is returned
	* @deprecated Remains here for compatability : use this->last_id
	* @return Integer/Boolean
	**/
	function lastInsertID(){
		return $this->last_id;
	}
}
?>